/*
	Figure S4 and S5: Pooled and Interacted Models by Race (Study 2)
	
	Figure S6 and S7: Pooled and Interacted Models by Party (Study 2)
*/

use data_study_2.dta, clear

// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_biden = 0
	label var zero_biden "Vote Share: 51%"
	gen zero_exp = 0
	label var zero_exp "Political newcomer"
	gen zero_dist = 0
	label var zero_dist "[63, 8, 13, 11, 5]"
	gen zero_racepol = 0
	label var zero_racepol "Not shown policy"
	
// Set regression variables
	loc reg_race "cand_black zero_race cand_asian cand_hispa"
	loc reg_sex "cand_female zero_sex"
	loc reg_exp "cand_exp_teach cand_exp_council cand_exp_lawyer cand_exp_business zero_exp"
	loc reg_biden "cand_biden_p59 cand_biden_p57 cand_biden_p55 cand_biden_p53 zero_biden"
	loc reg_distpop "cand_dist1 cand_dist2 cand_dist3 cand_dist4 cand_dist5 cand_dist6 zero_dist"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_racepol"
	loc reg_affirm2 "noracepolicy cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 "
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	
	loc int ""
	foreach r in white black asian hispa {
		gen `r'_aa1 = cand_policy_aa1*cand_`r'
		gen `r'_aa2 = cand_policy_aa2*cand_`r'
		gen `r'_aa3 = cand_policy_aa3*cand_`r'
		gen `r'_aa0 = noracepolicy*cand_`r'
		
		loc lab = proper("`r'")
		if "`lab'" == "Hispa" loc lab = "Hispanic"
	
		label var `r'_aa0 "`lab' X No Position"
		label var `r'_aa1 "`lab' X Expand"
		label var `r'_aa2 "`lab' X Keep"
		label var `r'_aa3 "`lab' X End"
		
		loc int "`int' `r'_aa0 `r'_aa1 `r'_aa2 `r'_aa3"
	
	}
	// set reference
	replace white_aa0 = 0
	label var noracepolicy "Not shown position"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white
		
//============================================================================== Regress the things

	
	// Respondent race
		eststo clear
		reg out_ideo `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_white == 1, vce(cluster r_id)
		eststo pool_ideo_w
		reg out_ideo `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_black == 1, vce(cluster r_id)
		eststo pool_ideo_b
		
		reg out_fair_bwdiff `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_white == 1, vce(cluster r_id)
		eststo pool_fair_w
		reg out_fair_bwdiff `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_black == 1, vce(cluster r_id)
		eststo pool_fair_b

	// PID
		reg out_ideo `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_dem == 1, vce(cluster r_id)
		eststo pool_ideo_d
		reg out_ideo `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_gop == 1, vce(cluster r_id)
		eststo pool_ideo_r
	
		loc b_pool_fair = string(round(_b[_cons], 0.001), "%9.3f")
		reg out_fair_bwdiff `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_dem == 1, vce(cluster r_id)
		eststo pool_fair_d
		reg out_fair_bwdiff `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if r_gop == 1, vce(cluster r_id)
		eststo pool_fair_r
		
	// Plot pooled model
		coefplot (pool_ideo_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
				 (pool_ideo_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(a) Ideological Liberalness}") || ///
				(pool_fair_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
				(pool_fair_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
				|| , drop(_cons cand_age) omitted baselevels ms(c) msize(medsmall) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black))  ///
			subtitle(, bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) note("`notes'", size(vsmall)) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(cand_black = "{bf: Race}" ///
					cand_female = "{bf: Gender}" ///
					cand_exp_teach = "{bf: Occupation}" ///
					cand_biden_p59 = "{bf: District Vote for Biden}" ///
					cand_dist1 = "{bf: District Racial % [W,B,A,H,O]}" ///
					cand_policy_abort1 = "{bf: Abortion}" ///
					cand_policy_tax1 = "{bf: Tax Policy}" ///
					cand_policy_health1 = "{bf: Healthcare}" ///
					cand_policy_eco1 = "{bf: Energy}" ///
					cand_policy_aa1 = "{bf: Affirmative Action}" ///
					, labsize(vsmall)) legend(order(2 "White" 4 "Black") size(vsmall)) 
	

		graph display, xsize(5) ysize(4.7) margins(vsmall)	
		graph export figure_s4.png, as(png) replace
		
	
		coefplot (pool_ideo_d, label() mc(navy) ciopts(color(navy) lw(med))) ///
				 (pool_ideo_r, label() mc(maroon) ciopts(color(maroon) lw(med))) ///
					, bylabel("{bf:(a) Ideological Liberalness}") || ///
				(pool_fair_d, label() mc(navy) ciopts(color(navy) lw(med))) ///
				(pool_fair_r, label() mc(maroon) ciopts(color(maroon) lw(med))) ///
					, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
				|| , drop(_cons cand_age) omitted baselevels ms(c) msize(medsmall) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black))  ///
			subtitle(, bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) note("`notes'", size(vsmall)) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(cand_black = "{bf: Race}" ///
					cand_female = "{bf: Gender}" ///
					cand_exp_teach = "{bf: Occupation}" ///
					cand_biden_p59 = "{bf: District Vote for Biden}" ///
					cand_dist1 = "{bf: District Racial % [W,B,A,H,O]}" ///
					cand_policy_abort1 = "{bf: Abortion}" ///
					cand_policy_tax1 = "{bf: Tax Policy}" ///
					cand_policy_health1 = "{bf: Healthcare}" ///
					cand_policy_eco1 = "{bf: Energy}" ///
					cand_policy_aa1 = "{bf: Affirmative Action}" ///
					, labsize(vsmall)) legend(order(2 "Democrats" 4 "Republicans") size(vsmall)) 
	
		graph display, xsize(5) ysize(4.7) margins(vsmall)	
		graph export figure_s6.png, as(png) replace
	
	
	// Effect of candidate attributes on main outcomes, interacted
		// Race
		reg out_ideo `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_white == 1, vce(cluster r_id)	
		eststo ideo_w
		reg out_ideo `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_black == 1, vce(cluster r_id)	
		eststo ideo_b
		
		reg out_fair_bwdiff `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_white == 1, vce(cluster r_id)
		eststo fair_bwdiff_w
		reg out_fair_bwdiff `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_black == 1, vce(cluster r_id)
		eststo fair_bwdiff_b
		
		
		// PID
		reg out_ideo `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_dem == 1, vce(cluster r_id)	
		eststo ideo_d
		reg out_ideo `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_gop == 1, vce(cluster r_id)	
		eststo ideo_r
		
		reg out_fair_bwdiff `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_dem == 1, vce(cluster r_id)
		eststo fair_bwdiff_d
		reg out_fair_bwdiff `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age if r_gop == 1, vce(cluster r_id)
		eststo fair_bwdiff_r

		// Plot
		coefplot (ideo_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
				 (ideo_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(a) Ideological Liberalness}") || ///
				(fair_bwdiff_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
				(fair_bwdiff_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
				|| , drop(_cons `reg_sex' `reg_issues' cand_age `reg_exp' `reg_biden' `reg_distpop') ///
				omitted baselevels ms(c) msize(medsmall) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black)) legend(order(2 "White" 4 "Black") size(vsmall))  ///
			subtitle(, bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(white_aa0 = "{bf: White X Affirmative Action}" ///
					black_aa0 = "{bf: Black X Affirmative Action}" ///
					asian_aa0 = "{bf: Asian X Affirmative Action}" ///
					hispa_aa0 = "{bf: Hispanic X Affirmative Action}" ///
					, labsize(vsmall)) 

		graph display, xsize(4.5) ysize(3.4) margins(vsmall)	
		graph export figure_s5.png, as(png) replace
		
	
		
		coefplot (ideo_d, label() mc(navy) ciopts(color(navy) lw(med))) ///
				 (ideo_r, label() mc(maroon) ciopts(color(maroon) lw(med))) ///
					, bylabel("{bf:(a) Ideological Liberalness}") || ///
				(fair_bwdiff_d, label() mc(navy) ciopts(color(navy) lw(med))) ///
				(fair_bwdiff_r, label() mc(maroon) ciopts(color(maroon) lw(med))) ///
					, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
				|| , drop(_cons `reg_sex' `reg_issues' cand_age `reg_exp' `reg_biden' `reg_distpop') ///
				omitted baselevels ms(c) msize(medsmall) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black)) legend(order(2 "Democrats" 4 "Republicans") size(vsmall))  ///
			subtitle(, bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(white_aa0 = "{bf: White X Affirmative Action}" ///
					black_aa0 = "{bf: Black X Affirmative Action}" ///
					asian_aa0 = "{bf: Asian X Affirmative Action}" ///
					hispa_aa0 = "{bf: Hispanic X Affirmative Action}" ///
					, labsize(vsmall)) 

	
		graph display, xsize(4.5) ysize(3.4) margins(vsmall)	
		graph export figure_s7.png, as(png) replace
		
	